from django.shortcuts import render

# Create your views here.
from drf_haystack.viewsets import HaystackViewSet
from rest_framework import filters
from rest_framework.generics import ListAPIView

from goods.serializers import SKUIndexSerializer
from . import serializers
from .models import SKU


class HotSkuListView(ListAPIView):
    serializer_class = serializers.HotSkuListSerializer
    pagination_class = None
    def get_queryset(self):
        category_id = self.kwargs.get("category_id")
        skus = SKU.objects.filter(category_id=category_id).order_by("-sales")[:3]
        return skus

class SkuListView(ListAPIView):
    serializer_class = serializers.HotSkuListSerializer

    filter_backends = (filters.OrderingFilter,)
    ordering_fields = ('create_time', 'price', "sales")

    def get_queryset(self):
        category_id = self.kwargs.get("category_id")
        skus = SKU.objects.filter(category_id=category_id)
        return skus

class SKUSearchViewSet(HaystackViewSet):
    index_models = [SKU]

    serializer_class = SKUIndexSerializer